package interview;

public class interview_16_04 {
    public String tictactoe(String[] board) {
        int n= board.length;;
        boolean flag=false;
        boolean[] col=new boolean[n];
        boolean[] row=new boolean[n];
        for(int i=0;i<n;++i){
            char a=board[i].charAt(0);
            char b=board[0].charAt(i);
            if(a==' ') {col[i]=true;flag=true;}
            if(b==' ') {row[i]=true;flag=true;}
            for(int j=1;j<n;++j){
                if(board[i].charAt(j)!=a&&board[i].charAt(j)!=' '){
                    col[i]=true;

                }
                if(board[i].charAt(j)==' ') {flag=true;col[i]=true;}
                if(board[j].charAt(i)!=b&&board[j].charAt(i)!=' '){
                    row[i]=true;
                }
                if(board[j].charAt(i)==' ') {flag=true;row[i]=true;}
            }
        }
        for(int i=0;i<n;++i){
            if(col[i]==false){
                return board[i].charAt(0)+"";
            }
            if(row[i]==false){
                return board[0].charAt(i)+"";
            }
        }
        row[0]=false;
        char a=board[0].charAt(0);
        if(a==' ') {row[0]=true;}
        for(int i=1;i<n;++i){
            if(board[i].charAt(i)!=a){
                row[0]=true;
            }
        }
        if(row[0]==false){
            return board[0].charAt(0)+"";
        }
        row[0]=false;
        a=board[0].charAt(n-1);
        if(a==' ') {row[0]=true;}
        for(int i=1;i<n;++i){
            if(board[i].charAt(n-1-i)!=a) row[0]=true;
        }
        if(row[0]==false){
            return board[0].charAt(n-1)+"";
        }
        if(flag) return "Pending";
        return "Draw";
    }
}
